//
//  SearchProductModel.swift
//
//  Created by eme on 2017/1/3
//  Copyright (c) . All rights reserved.
//

import Foundation
import SwiftyJSON
import ModelProtocol

protocol Copyable {
    func copy() -> Copyable
}

class SearchProductModel: ModelProtocol ,Copyable {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kSearchProductBrlistKey: String = "brlist"
    internal let kSearchProductPtlistKey: String = "ptlist"
    internal let kSearchProductPalistKey: String = "palist"
    internal let kSearchProductProductlistKey: String = "productlist"
    
    
    // MARK: 属性
    var brlist: [BrlistModel]
    var ptlist: [PtlistModel]
    var palist: [PalistModel]
    var productlist: [ProductlistModel]
    
    
    /// 保存已选中的品牌model
    var hasSelected_BrModel:BrlistModel?
    /// 保存已选中的类目model
    var hasSelected_PtModel:PtlistModel?
    ///起批量
    var moq:String = ""
    /// 价格区间最多
    var maxamount:String = ""
    /// 价格区间 最少
    var minamount:String = ""
    /// 用于显示选中的属性值
    var showPavstr:String {
        get{
            var relArr:[String] = []
            for item in palist{
                if item.showSubStr != "不限" {
                    relArr.append(item.showSubStr)
                }
            }
            if relArr.count > 0 {
                return  relArr.joined(separator: ",")
            }else{
                return "不限"
            }
        }
    }
    /// 用于传的值
    var pavstr:String{
        get{
            var relArr:[String] = []
            for item in palist{
                for subitem in item.pavalues{
                    if subitem.hasSelected {
                        relArr.append(subitem.pavalue)
                    }
                }
                
            }
            if relArr.count > 0 {
                return  relArr.joined(separator: ",")
            }else{
                return ""
            }
        }
    }
    var sxParam:(brid:String,ptid:String,pavstr:String,minamount:String,maxamount:String,moq:String){
        get{
            var ptid = ""
            var brid = ""
            if let brModel = self.hasSelected_BrModel {
                brid = brModel.brid
            }
            if let ptModel = self.hasSelected_PtModel {
                ptid = "\(ptModel.ptid)"
            }
            return (brid:brid,ptid:ptid,pavstr:self.pavstr,minamount:self.minamount,maxamount:self.maxamount,moq:self.moq)
        }
    }
    func copy() -> Copyable {
        let searchModel = SearchProductModel(json: "")!
        searchModel.brlist = self.brlist.map{ $0.copy() as! BrlistModel}
        searchModel.ptlist = self.ptlist.map{ $0.copy() as! PtlistModel}
        searchModel.palist = self.palist.map{ $0.copy() as! PalistModel}
        searchModel.hasSelected_BrModel = self.hasSelected_BrModel
        searchModel.hasSelected_PtModel = self.hasSelected_PtModel
        searchModel.moq = self.moq
        searchModel.maxamount = self.maxamount
        searchModel.minamount = self.minamount
        return searchModel
    }
    
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    required  init?(json: JSON) {
        brlist = []
        if let items = json[kSearchProductBrlistKey].array {
            for item in items {
                if let brlistModel = BrlistModel(json: item){
                    brlist.append(brlistModel)
                }
            }
        }
        ptlist = []
        if let items = json[kSearchProductPtlistKey].array {
            for item in items {
                if let ptlistModel = PtlistModel(json: item){
                    ptlist.append(ptlistModel)
                }
            }
        }
        palist = []
        if let items = json[kSearchProductPalistKey].array {
            for item in items {
                if let palistModel = PalistModel(json: item){
                    palist.append(palistModel)
                }
            }
        }
        productlist = []
        if let items = json[kSearchProductProductlistKey].array {
            for item in items {
                if let productlistModel = ProductlistModel(json: item){
                    productlist.append(productlistModel)
                }
            }
        }
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        if brlist.count > 0 {
            var temp: [AnyObject] = []
            for item in brlist {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kSearchProductBrlistKey)
        }
        if ptlist.count > 0 {
            var temp: [AnyObject] = []
            for item in ptlist {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kSearchProductPtlistKey)
        }
        if palist.count > 0 {
            var temp: [AnyObject] = []
            for item in palist {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kSearchProductPalistKey)
        }
        if productlist.count > 0 {
            var temp: [AnyObject] = []
            for item in productlist {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kSearchProductProductlistKey)
        }
        
        return dictionary
    }
    
    
}
